package suo;

import java.util.Map;
import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;

public class Main implements Runnable {
    public  static ReentrantLock lock = new ReentrantLock();

    public static int i=0;

    @Override
    public void run() {
        lock.lock();
        try {
            Thread.sleep((long) Math.random());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        i++;
        lock.unlock();
    }

    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[1000];
        for (int i = 0; i < 1000; i++) {
            threads[i] = new Thread(new Main());
            threads[i].start();
        }
        // 等待所有线程完成
        for (Thread t : threads) {
            t.join();
        }
        System.out.println(i); // 现在会正确输出1000
    }
}
